/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       MdmsUserController.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/11/11      feiwe                         add
 */


package com.fengwei.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengwei.form.MapForObject.MapForObject;
import com.fengwei.form.MdmsUser;
import com.fengwei.requestdata.user.MdmsUserAddData;
import com.fengwei.requestdata.user.MdmsUserChangePasswordData;
import com.fengwei.requestdata.user.MdmsUserDelData;
import com.fengwei.requestdata.user.MdmsUserEditData;
import com.fengwei.requestdata.user.MdmsUserPagingListData;
import com.fengwei.requestdata.user.MdmsUserResetMfaKeyData;
import com.fengwei.requestdata.user.MdmsUserResetPasswordData;
import com.fengwei.service.ConsoleCommonService;
import com.fengwei.service.MdmsUserService;
import com.fengwei.util.PltResult;
import com.fengwei.util.ResponseUtil;
import com.fengwei.verify.Verify;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class MdmsUserController {
    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private MdmsUserService mdmsUserService;

    @Autowired
    private ConsoleCommonService consoleCommonService;

    @GetMapping(value = "/mdmsUser/loadPage")
    public String loadPage(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "mdmsUser";
    }

    /**
     * 用户列表
     */
    @PostMapping(path = "/mdmsUser/pagingList")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> pagingList(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsUserPagingListData mdmsUserPagingListData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });

        // 校验传入参数
        Verify.verifyData(mdmsUserPagingListData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserService.pagingList(mdmsUserPagingListData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 添加用户
     */
    @PostMapping(path = "/mdmsUser/add")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> add(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsUserAddData mdmsUserAddData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsUserAddData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsUserAddData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserService.add(mdmsUserAddData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 修改用户
     */
    @PostMapping(path = "/mdmsUser/edit")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> edit(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsUserEditData mdmsUserEditData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsUserEditData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsUserEditData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserService.edit(mdmsUserEditData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 删除用户
     */
    @PostMapping(path = "/mdmsUser/del")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> del(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsUserDelData mdmsUserDelData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<MdmsUserDelData>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsUserDelData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserService.del(mdmsUserDelData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 密码修改
     */
    @PostMapping(path = "/mdmsUser/changePassword")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN','DBA','USER')")
    public Map<String, Object> changePassword(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsUserChangePasswordData mdmsUserChangePasswordData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });

        // 校验传入参数
        Verify.verifyData(mdmsUserChangePasswordData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserService.changePassword(mdmsUserChangePasswordData, request, mdmsUser);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 密码重置
     */
    @PostMapping(path = "/mdmsUser/resetPassword")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> resetPassword(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsUserResetPasswordData mdmsUserResetPasswordData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });

        // 校验传入参数
        Verify.verifyData(mdmsUserResetPasswordData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserService.resetPassword(mdmsUserResetPasswordData, request, mdmsUser);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 密码重置MFA密钥
     */
    @PostMapping(path = "/mdmsUser/resetMfaKey")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> resetMfaKey(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsUserResetMfaKeyData mdmsUserResetMfaKeyData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });

        // 校验传入参数
        Verify.verifyData(mdmsUserResetMfaKeyData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserService.resetMfaKey(mdmsUserResetMfaKeyData, request, mdmsUser);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }
}
